//
//  UserManagerController.m
//  myapp
//
//  Created by 安建伟 on 2019/7/31.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "UserManagerController.h"
#import "UserManagerViewCell.h"
#import "LogOutViewCell.h"
#import "LoginViewController.h"
#import "ModifyPasswordController.h"

NSString *const CellIdentifier_Normal = @"CellID_Normal";
NSString *const CellIdentifier_Logout = @"CellID_Logout";

static CGFloat const HEIGHT_CELL = 51.f;
static float HEIGHT_HEADER = 40.f;

@interface UserManagerController () <UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) UITableView *accountTable;

@end

@implementation UserManagerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1.0];
    self.navigationItem.title = LocalString(@"用户管理");
    
    self.accountTable = [self accountTable];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.rdv_tabBarController setTabBarHidden:YES animated:YES];
    
}

#pragma mark - Lazy load
- (UITableView *)accountTable{
    if (!_accountTable) {
        _accountTable = ({
            UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
            tableView.backgroundColor = [UIColor clearColor];
            tableView.dataSource = self;
            tableView.delegate = self;
            [tableView registerClass:[UserManagerViewCell class] forCellReuseIdentifier:CellIdentifier_Normal];
            [tableView registerClass:[LogOutViewCell class] forCellReuseIdentifier:CellIdentifier_Logout];
            tableView.separatorColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.08];
            
            [self.view addSubview:tableView];
            
            tableView.scrollEnabled = NO;
            tableView;
        });
    }
    return _accountTable;
}

#pragma mark - UITableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0){
        return 2;
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        UserManagerViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier_Normal];
        if (cell == nil) {
            cell = [[UserManagerViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier_Normal];
        }
        if (indexPath.row == 0) {
            cell.leftLabel.text = LocalString(@"用户账号");
            cell.rightLabel.text = [GizManage shareInstance].userPhone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else{
            cell.leftLabel.text = LocalString(@"修改登录密码");
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            return cell;
        }
    }else{
        LogOutViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier_Logout];
        if (cell == nil) {
            cell = [[LogOutViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier_Logout];
        }
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0){
        if (indexPath.row == 1) {
            ModifyPasswordController *modifyPasswordVC = [[ModifyPasswordController alloc] init];
            [self.navigationController pushViewController:modifyPasswordVC animated:YES];
        }
    }else{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:LocalString(@"退出登录") message:LocalString(@"请再次确认是否退出登录") preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self.navigationController presentViewController:loginVC animated:YES completion:nil];
            
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:confirmAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT_CELL;
}

//section头部间距

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return HEIGHT_HEADER;//section头部高度
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
    view.backgroundColor = [UIColor clearColor];
    return view ;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 15;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

@end
